/*
Eteria IRC Client, an RFC 1459 compliant client program written in Java.
Copyright (C) 2001  Christian Buck <cbuck at lantis.de>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.MessageFormat;
import ar.com.jkohen.awt.ImageButton;
import ar.com.jkohen.util.Resources;

public class ModeList extends Dialog implements ActionListener
{
    private EIRC eirc;
	private Frame f;
    private java.awt.List items;
	private ImageButton b;

	private Resources res;

	private String chan;
	private char mode;
	private static int MODE_NUMBER = 4;

    public ModeList(EIRC eirc, Frame f)
	{
    	super(f);

    	this.eirc = eirc;
		this.f = f;

		setFont(eirc.getFont());

        GridBagLayout gb = new GridBagLayout();
    	GridBagConstraints gbc = new GridBagConstraints();
        setLayout(gb);
        gbc.insets = new Insets(2, 2, 2, 2);

        items = new java.awt.List(10, true);
		gbc.anchor = GridBagConstraints.CENTER;
      	gbc.weightx = 1;
    	gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
    	gb.setConstraints(items, gbc);
    	add(items);

        b = new ImageButton(res.getString("ok"));
    	b.setActionCommand("ok");
		gbc.anchor = GridBagConstraints.EAST;
    	gbc.weighty = 0;
        gbc.fill = GridBagConstraints.NONE;
		gbc.gridwidth = 1;
        gb.setConstraints(b, gbc);
    	add(b);
        b.addActionListener(this);

        b = new ImageButton();
    	b.setActionCommand("delete");
		gbc.anchor = GridBagConstraints.WEST;
        gb.setConstraints(b, gbc);
    	add(b);
        b.addActionListener(this);

		setResizable(true);
    }

	public void initList(String chan, boolean is_op, Vector data, String title, MessageFormat msg, String button, char mode)
	{
		setTitle(title);
    	this.chan = chan;
		this.mode = mode;

    	items.removeAll();
      	for (Enumeration e = data.elements(); e.hasMoreElements(); )
      	{
    	    ListItem bi = (ListItem) e.nextElement();
			Object [] ob = { bi.getMask(), bi.getOp(), bi.getDate() };
    	    items.add(msg.format(ob));
        }
		b.setText(button);
		b.setEnabled(is_op);
		show();
	}

    public void setBackground(Color c)
    {
        super.setBackground(c);
    }

    public void setForeground(Color c)
    {
        super.setForeground(c);
    }

    public void setSelectedBackground(Color c)
    {
    }

    public void setSelectedForeground(Color c)
    {
    }

    public void actionPerformed(ActionEvent ev)
    {
		String ac = ev.getActionCommand();
		String masks[] = items.getSelectedItems();

		if (ac.equals("ok"))
		{
			processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
		}
		if (ac.equals("delete") && masks.length > 0)
		{
			String params[] = new String[2 + MODE_NUMBER];
			params[0] = chan;
			for (int i = 0; i < MODE_NUMBER; i++)
				params[2 + i] = "";

			for (int i = 0; i < masks.length; i += MODE_NUMBER)
			{
				params[1] = "-";

				for (int j = 0; j < MODE_NUMBER && (i + j) < masks.length; j++)
				{
					params[1] += mode;
					params[2 + j] = masks[i + j].substring(0, masks[i + j].indexOf(' '));
					items.remove(masks[i + j]);
				}
				eirc.sendMessage("MODE", params);
			}
		}
    }

	public void show()
	{
		setBackground(eirc.getBackground());
		setForeground(eirc.getForeground());

		pack();
       	setLocation((f.getSize().width - getSize().width) / 2 + f.getLocation().x, (f.getSize().height - getSize().height) / 2 + f.getLocation().y);
		super.show();
	}
}
